{% load i18n %}
{% with choices=field_info.choices label=field_info.label %}
<div class="modal fade" id="id_{{ name }}" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span></button>
        <h4 class="modal-title">{{ label }} {% trans "选项" %}</h4>
      </div>
      <div class="modal-body">
        <table id="choice-table-{{ name }}" class="table table-bordered table-hover table-condensed table-striped">
          <thead>
            <tr>
              <th>{% trans "导入值" %}</th>
              <th>{% trans "标签" %}</th>
            </tr>
          </thead>
          <tbody>
            {% for choice in choices %}
            <tr>
              <td><code>{{ choice.value }}</code></td>
              <td>{{ choice.label }}</td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      <div class="modal-footer">
        <div class="text-left">
          <button type="button" class="btn btn-default btn-sm" onclick="copyTableContent('choice-table-{{ name }}')">
            <i class="fa fa-copy"></i> {% trans "复制选项内容" %}
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
function copyTableContent(tableId) {
    const table = document.getElementById(tableId);
    let text = '';
    
    // 获取表头
    const headers = table.getElementsByTagName('th');
    for (let i = 0; i < headers.length; i++) {
        text += headers[i].textContent.trim();
        text += (i === headers.length - 1) ? '\n' : '\t';
    }
    
    // 获取表格数据
    const rows = table.getElementsByTagName('tr');
    for (let i = 1; i < rows.length; i++) { // 从1开始跳过表头行
        const cells = rows[i].getElementsByTagName('td');
        for (let j = 0; j < cells.length; j++) {
            // 获取纯文本内容，移除code标签
            text += cells[j].textContent.trim();
            text += (j === cells.length - 1) ? '\n' : '\t';
        }
    }
    
    // 创建临时textarea元素来复制内容
    const textarea = document.createElement('textarea');
    textarea.value = text;
    document.body.appendChild(textarea);
    textarea.select();
    
    try {
        document.execCommand('copy');
        alert('{% trans "表格内容已复制到剪贴板" %}');
    } catch (err) {
        console.error('复制失败:', err);
        alert('{% trans "复制失败，请重试" %}');
    } finally {
        document.body.removeChild(textarea);
    }
}
</script>
{% endwith %}